package com.onpositive.mediawiki.serializing;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.LinkedHashSet;

import com.onpositive.data.serializer.writers.StringWriter;
import com.onpositive.units.ParsedValue;
import com.onpositive.units.ParsedValueSequenceV2;

public class ValueSequenceWriter extends AbstractMediaWikiWriter<ParsedValueSequenceV2>
{
	StringWriter sw = new StringWriter() ;
	
	public ValueSequenceWriter()
	{
		super(ParsedValueSequenceV2.class);
	}

	public static String valueSeparator = "_#_" ;//"" + (char)0 ;
	@Override
	protected void append(ParsedValueSequenceV2 obj) {
		
		if( obj == null )
		{
			sw.append( null );
			return ;
		}
		
		ParsedValue[] values = obj.getValuesArray() ;
		
		StringBuilder builder = new StringBuilder() ;
		
		for( ParsedValue pv : values )
		{
			builder.append( pv.getValueAsString() ) ;
			builder.append( valueSeparator ) ;
		}

		int ind = builder.length() - valueSeparator.length();
		if( ind >= 0 )
			builder.delete( ind, builder.length() ) ;
		
		sw.append( builder.toString() );		
	}

	@Override
	public void serialize(DataOutputStream stream) throws IOException {
		sw.serialize(stream) ;		
	}
}